package com.example.demo.service.Impl;

import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.example.demo.mapper.ProjectMapper;
import com.example.demo.result.ProjectInfo;
import com.example.demo.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectMapper projectMapper ;
    @Autowired
    FunctionPointMapper functionPointMapper;
    @Autowired
    ProMemberMapper proMemberMapper;
    @Autowired
    UserMapper userMapper;

    //新建项目
    @Override
    public int addProject(Project project){
        return projectMapper.insert(project);
    }

    //得到项目信息
    @Override
    public Project getInfo(int projectId){
        return projectMapper.selectByPrimaryKey(projectId);
    }

    //结束项目
    @Override
    public int stopProject(int projectId, int proState, Date proData){
        Project project = new Project();
        project.setProId(projectId);
        project.setProState(proState);
        project.setProDate(proData);
        return projectMapper.updateByPrimaryKey(project);
    }

    //查看功能点
    @Override
    public List<FunctionPoint> getFunctionPointByProjectId(Integer functionProId) {
        FunctionPointExample functionPointExample=new FunctionPointExample();
        FunctionPointExample.Criteria criteria=functionPointExample.createCriteria();
        criteria.andFunctionProIdEqualTo(functionProId);
       return functionPointMapper.selectByExample(functionPointExample);
    }

    @Override
    public List<Project> getUserProjectListByUserId(int userId) {
        ProMemberExample promemberexample = new ProMemberExample();

        ProMemberExample.Criteria criteria = promemberexample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<ProMember> list = proMemberMapper.selectByExample(promemberexample);
        System.out.println(list);
        List<Integer> companyIds = new ArrayList<>();

        for (int i = 0;i <list.size();i++){
            companyIds.add(list.get(i).getProId());
            System.out.println(companyIds.get(i));
        }
        ProjectExample projectExample = new ProjectExample();

        ProjectExample.Criteria criteria1 = projectExample.createCriteria();
        criteria1.andProIdIn(companyIds);
        List<Project> projects = projectMapper.selectByExample(projectExample);
        System.out.println(projects);
        return projects;
    }

    @Override
    public List<User> getProMemberListByProId(Integer proId) {
        List<Integer> userIds = new ArrayList<>();
        ProMemberExample proMemberExample = new ProMemberExample();
        ProMemberExample.Criteria criteria = proMemberExample.createCriteria();
        criteria.andProIdEqualTo(proId);
        List<ProMember> proMembers = proMemberMapper.selectByExample(proMemberExample);//根据项目ID获取成员列表
        System.out.println(proMembers);
        if (proMembers.isEmpty())
            return Collections.EMPTY_LIST;

        for (ProMember proMember: proMembers){
            userIds.add(proMember.getUserId()); //获取该项目成员的userId
        }
        System.out.println(userIds);

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria1 = userExample.createCriteria();
        criteria1.andUserIdIn(userIds);
        List<User> users = userMapper.selectByExample(userExample);
        System.out.println(users);
        return users;
    }

    @Override
    public int addProMember(Integer proUserId, Integer proId) {
        ProMember proMember = new ProMember();
        proMember.setProId(proId);
        proMember.setUserId(proUserId);
        return proMemberMapper.insert(proMember);
    }

    @Override
    public int deleteProMember(Integer proUserId, Integer proId) {
        ProMemberExample proMemberExample = new ProMemberExample();
        ProMemberExample.Criteria criteria = proMemberExample.createCriteria();
        criteria.andUserIdEqualTo(proUserId).andProIdEqualTo(proId);
        return proMemberMapper.deleteByExample(proMemberExample);
    }

    @Override
    public long proMemberNum(Integer proId) {
        ProMemberExample proMemberExample = new ProMemberExample();
        ProMemberExample.Criteria criteria = proMemberExample.createCriteria();
        criteria.andProIdEqualTo(proId);
        return proMemberMapper.countByExample(proMemberExample);
    }
}
